package com.lizard.schedule.utils.encrypt;

import java.security.MessageDigest;

/**
 * MD5加密工具类
 */
public class MD5Util {

    /**
     * 取16位的MD5值
     * @param string
     * @return
     */
    public static String md5EncryptL16(String string){
        String ret = null;
        String md5 = md5Encrypt(string);
        if(md5 != null){
            ret = md5.substring(8, 24);
        }
        return ret;
    }

    /**
     * MD5加密
     *
     * @param string
     * @return
     */
    public static String md5Encrypt(String string){

        String s = null;
        if (string==null||"".equals(string)) {
            return string;
        }
        byte[] b;
        StringBuilder sBuilder = new StringBuilder();
        try {
            b = string.getBytes("UTF-8");
            MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(b);
            b = mDigest.digest();

            for (byte c : b) {
                s = Integer.toHexString(c&0xff);
                if (s.length()<2) {
                    sBuilder.append("0");
                }
                sBuilder.append(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sBuilder.toString();
    }

}
